import Vue from "vue";
import VueRouter from 'vue-router';
Vue.use(VueRouter);

import routes from './routes';

const router = new VueRouter({
    routes,
    scrollBehavior(to, from, savedPosition) {
        return {
            x: 0,
            y: 0
        }
    }
});


// 配置前置守卫
router.beforeEach(async (to, from, next) => {
    const token = localStorage.getItem('token');
    if (token) {
        if (to.path == '/other-login' || to.path == '/register' || to.path == '/login') {
            next('/home');
        } else {
            next();
        }
    } else {
        next();
    }
})

export default router;